import axios from 'axios';

const API_BASE_URL = 'http://localhost:5173';

export const api = {
  async get(endpoint : string, params?:Record<string,any>) {
    try {
      const url = `${API_BASE_URL}${endpoint}`;
      const response = await axios.get(url, { params });
      return response.data;
    } catch (error) {
      throw new Error('网络请求失败');
    }
  },

  async post(endpoint : string, data : any) {
    try {
      const url = `${API_BASE_URL}${endpoint}`;
      const response = await axios.post(url, data, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
      return response.data;
    } catch (error) {
      throw new Error('网络请求失败');
    }
  },

  async put(endpoint : string, data : any) {
    try {
      const url = `${API_BASE_URL}${endpoint}`;
      const response = await axios.put(url, data, {
        headers: {
          'Content-Type': 'application/json'
        }
      });
      return response.data;
    } catch (error) {
      throw new Error('网络请求失败');
    }
  },

  async delete(endpoint : string) {
    try {
      const url = `${API_BASE_URL}${endpoint}`;
      const response = await axios.delete(url);
      return response.data;
    } catch (error) {
      throw new Error('网络请求失败');
    }
  },
};